package com.tshopping.service.impl;

import com.tshopping.bean.Comment;
import com.tshopping.bean.DataModel;
import com.tshopping.bean.Page;
import com.tshopping.dao.CommentDao;
import com.tshopping.dao.OrderDetailsDao;
import com.tshopping.dao.PictureDao;
import com.tshopping.service.CommentService;
import com.tshopping.util.FileUploader;
import com.tshopping.util.ResponseData;
import com.tshopping.util.TimeProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.UUID;

/**
 * @name:
 * @description: 商品评论业务层类
 * @author: Jackeys
 * @date: 2021-01-31 23:01
 */
@Service(value = "commentService")
@Transactional
public class CommentServiceImpl implements CommentService {


    @Autowired
    private CommentDao commentDao;

    @Autowired
    private PictureDao pictureDao;

    @Autowired
    private OrderDetailsDao orderDetailsDao;

    @Override
    /**
     * @description 用户评论商品
     * @Param: dataModel
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-31 23:05
     */
    public ResponseData addComment(DataModel dataModel) throws ParseException {
        ResponseData responseData=ResponseData.ok();
        Comment comment=dataModel.getComment();
        System.out.println(comment);
        String time= TimeProvider.getTime();
        comment.setTime(time);
        ArrayList<String> pictures=dataModel.getPictures();
        if(!commentDao.insertGoodsByGoodsInformation(comment)){
            responseData.putDataValue("flag",false);
            return responseData;
        }
        for(String picture:pictures){
            if(!pictureDao.insertIntoCommentPictures(comment.getId(),picture)){
                responseData.putDataValue("flag",false);
                return responseData;
            }
        }
        if(!orderDetailsDao.updateCommentById(1,comment.getDetail())){
            responseData.putDataValue("flag",false);
            return responseData;
        }
        responseData.putDataValue("flag",true);
        return responseData;
    }

    @Override
    /**
     * @description 获取商品评论
     * @Param: goods
     * @Param: currentPage
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-31 23:14
     */
    public ResponseData selectComment(int goods, int currentPage) {
        ResponseData responseData = ResponseData.ok();
        Page page = new Page();
        //1、当前页数
        page.setCurrentPage(currentPage);

        //2、每页显示的数据
        int pageSize = 5;
        page.setPageSize(pageSize);

        //3、总记录数
        int totalCount = commentDao.selectCountByGoodsId(goods);
        page.setTotalCount(totalCount);

        //4、根据总记录数和每页显示的记录数计算总页数
        double tc = totalCount;
        Double num = Math.ceil(tc / pageSize);//向上取整
        page.setTotalPage(num.intValue());

        int start = (currentPage - 1) * pageSize;
        int size = pageSize;
        ArrayList<Comment> comments=commentDao.selectGoodsCommentsByGoodsId(goods,start,size);
        ArrayList<ArrayList<String>> pictureList=new ArrayList<>();
        for (Comment comment:comments){
            pictureList.add(pictureDao.selectPicturesByGoods(comment.getGoods()));
        }
        responseData.putDataValue("page", page);
        responseData.putDataValue("commentList",comments);
        responseData.putDataValue("pictureList",pictureList);
        return responseData;
    }

    @Override
    /**
     * @description 上传评论图片
     * @Param: request
     * @Param: email
     * @Param: file
     * @return ResponseData
     * @author Jackeys
     * @date 2021-02-01 00:02
     */
    public ResponseData uploadCommentPicture(HttpServletRequest request, String email, MultipartFile file) {
        ResponseData responseData=ResponseData.ok();
        String directoryPath = request.getSession().getServletContext().getRealPath("resource/users/" + email + "/commentPicture/");
        String originalFileName = file.getOriginalFilename();
        String newFileName = UUID.randomUUID() + originalFileName;
        String path = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/resource/users/" + email + "/commentPicture/" + newFileName;
        if(FileUploader.upload(directoryPath, newFileName, file)) {
            responseData.putDataValue("pictureURL",path);
        } else {
            responseData.putDataValue("pictureURL",null);
        }
        return responseData;
    }
}
